﻿using WebApp.BLL.Base;
using WebApp.DAL;
using WebApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace WebApp.BLL.Manage
{
    public class S_dicttypeService : BaseService<S_dicttype>
    {

        #region 构造

        public S_dicttypeService() : base(new S_dicttypeRepository()) { }
        internal S_dicttypeRepository dicttypeRepository = new S_dicttypeRepository();
        #endregion

        #region GetSelectList
        /// <summary>
        /// 获取数据字典下拉框类型
        /// </summary>
        /// <param name="typeCode"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public SelectList GetSelectList(string selectedValue = "")
        {
            var list = this.CurrentRepository.Entities
                .OrderBy(p => p.Typecode)
                .Select(p => new
                {
                    value = p.Typecode,
                    text = p.Typecode + "(" + p.Typename_cn + ")"
                })
                .ToList();

            return new SelectList(list, "value", "text", selectedValue);
        }

        #endregion

        #region GetDictName

        /// <summary>
        /// 根据代码获取名称
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string GetDicttypeName(string code)
        {
            var item = this.CurrentRepository.Entities
                .FirstOrDefault(p => p.Typecode == code);

            if (item == null)
                return string.Empty;
            else
                return item.Typename_cn;
        }

        #endregion

        #region TableTreeById

        public IList<S_dicttype> TableTreeById(string tableid)
        {
            return this.CurrentRepository.Entities
                       .OrderBy(p => p.Sort)
                       .ToList();
        }
        #endregion
    }
}
